﻿package com.andyage.admin.bean;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.pingplusplus.model.Charge;

/**
 * 所有返回数据的描述类
 * @author liuyayun
 *
 */
public class ResultDescription {
	
	/**
	 * 对结果的描述
	 */
	public String description = null;
	/**
	 * 状态码
	 * 失败0，成功1，未登录2
	 */
	public int status = 0;
	
	/**
	 * 成功
	 */
	public static int SUCCESS = 1;
	/**
	 * 失败
	 */
	public static int FAILUER = 0;
	/**
	 * 未登录
	 */
	public static int UNLOGIN = 2;
	/**
	 * 未注册
	 */
	public static int UNREGISTER = 3;
	/**
	 * 短信验证码错误
	 */
	public static int SMSERROR = 4;
	/**
	 * 没有ticket
	 */
	public static int NOTICKET = 5;
	/**
	 * 没有openid
	 * 这个是在使用微信支付的时候，如果没有openid，那么就需要跳转code页面去获取openid
	 */
	public static int NOOPENID = 6;
	
	/**
	 * 返回的数据（json格式）
	 */
	public Object data = null;
	
	public ResultDescription() {
		// TODO Auto-generated constructor stub
	}
	/**
	 * 转换为标准的json格式
	 * @return
	 */
	public String toJson()
	{
		String result = "{";
		result += "\"status\":" + this.status + "";
		if(this.description!=null)
		{
			result += ",\"description\":\"" + this.description + "\"";
		}
		else
		{
			result += "\"description\":null";
		}
		if(this.data!=null)
		{
			if(this.data instanceof Charge)
			{
				result += ",\"data\":" + this.data.toString().replaceAll("\r|\n|\t|\\s", "");
			}
			else
			{
				GsonBuilder gb =new GsonBuilder();
				gb.disableHtmlEscaping();
				gb.setDateFormat("yyyy-MM-dd HH:mm:ss");
				Gson gson = gb.create(); 
				//有错，不能识别
				result += ",\"data\":" + gson.toJson(this.data);	
			}
			
		}
		else
		{
			result += ",\"data\":null";
		}
		result += "}";
		return result;
	}
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Object getData() {
		return data;
	}
	public void setData(Object data) {
		this.data = data;
	}
}
